﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Gui.Functies.Takenlijst_windows
{
    public partial class Addlid : UserControl
    {
        //projectID uit vorige scherm doorgestuurd
        private int projectID;
        private string status;
        string userID;
        string taakID;
        private Hashtable takenBezig;
        private Hashtable takenKlaar;
        private Hashtable takenNietKlaar;
        private Hashtable projectLeden;

        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");

        public Addlid()
        {
            
        }

        public Addlid(int projectnummer, Hashtable bezig, Hashtable klaar, Hashtable nietKlaar, Hashtable leden)
        {
            projectID = projectnummer;
            takenBezig = bezig;
            takenKlaar = klaar;
            takenNietKlaar = nietKlaar;
            projectLeden = leden;
            InitializeComponent();
            getNames();
            getTasks();
        }

        private void getNames()
        {
            comboBox1.Items.Clear();

            foreach (string key in projectLeden.Keys)
            {
                comboBox1.Items.Add(String.Format("{0}", key, projectLeden[key]));
            }
        }

        private void getTasks()
        {
            comboBox2.Items.Clear();
            //taken weergeven die niet klaar zijn
            foreach (string key in takenNietKlaar.Keys)
            {
                comboBox2.Items.Add(String.Format("{0}", key, takenNietKlaar[key]));
            }

            //taken weergeven die bezig zijn
            foreach (string key in takenBezig.Keys)
            {
                comboBox2.Items.Add(String.Format("{0}", key, takenBezig[key]));
            }

            //taken weergeven die klaar zijn
            foreach (string key in takenKlaar.Keys)
            {
                comboBox2.Items.Add(String.Format("{0}", key, takenKlaar[key]));
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            string user = comboBox1.SelectedItem.ToString();
            string taak = comboBox2.SelectedItem.ToString();

            if (user != null && taak != null)
            {
                //id moet opgehaald worden aan de hand van de status van de taak
                MySqlCommand checkStatus = new MySqlCommand("SELECT Status FROM taken WHERE ProjectID='"+projectID+"'AND Taak='"+taak+"'", grabConnection);
                try
                {
                    grabConnection.Open();
                    MySqlDataReader reader = checkStatus.ExecuteReader();

                    while (reader.Read())
                    {
                        status = reader["Status"].ToString();
                    }

                    if (status == "Bezig")
                    {
                        userID = projectLeden[user].ToString();
                        taakID = takenBezig[taak].ToString();
                    }
                    else
                    {
                        if (status == "Klaar")
                        {
                            userID = projectLeden[user].ToString();
                            taakID = takenKlaar[taak].ToString();
                        }
                        else
                        {
                            if (status == "Niet klaar")
                            {
                                userID = projectLeden[user].ToString();
                                taakID = takenNietKlaar[taak].ToString();
                            }
                        }
                    }

                    reader.Dispose();
                    checkStatus.Dispose();
                    grabConnection.Close();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                              
                MySqlCommand attachTask = new MySqlCommand("INSERT INTO taak_lid (TaakID, UserID)" + "VALUES ('"+taakID+"','"+userID+"')", grabConnection);
                try
                {
                    grabConnection.Open();
                    attachTask.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    grabConnection.Close();
                    this.Controls.Clear();
                    this.Controls.Add(new Takenlijst(projectID));
                }
            }
        }
    }
}